package com.ssyt.yt1.day24.statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ssyt.yt1.common.Constant;

/**
 * DQL:查询
 *
 * @author Hsu.
 * @since 2015年12月25日
 */
public class DQLDemo {

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {
		// 1、加载驱动
		Class.forName(Constant.DRIVER);

		// 2、连接数据库
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = DriverManager.getConnection(Constant.URL,
					Constant.USER, Constant.PASSWORD);
			// 3、操作数据库
			statement = connection.createStatement();
			String sql = "SELECT * FROM user";
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				// 注意跟数据库的字段名对应
				// System.out.println(resultSet.getString("username"));
				// 注意跟数据库的字段顺序对应，从1开始
				// System.out.println(resultSet.getString(3));

				System.out.println(resultSet.getInt("id") + ", "
						+ resultSet.getString("idcard") + ", "
						+ resultSet.getString("username") + ", "
						+ resultSet.getString("password") + ", "
						+ resultSet.getBigDecimal("salary") + ", "
						+ resultSet.getDate("birthday") + ", "
						+ resultSet.getString("memo"));
			}
		} finally {
			// 4、关闭资源
			if (resultSet != null) {
				resultSet.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (connection != null) {
				connection.close();
			}
		}
	}
}
